package com.xrit.sunnyday.security;


import com.alibaba.dubbo.config.annotation.Reference;
import com.xrit.sunnyday.pojo.Permission;
import com.xrit.sunnyday.pojo.Role;
import com.xrit.sunnyday.pojo.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Component
@Transactional
public class UserService implements UserDetailsService {

    @Reference
    private com.xrit.sunnyday.service.UserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userService.findByUsername(username);
        //判断用户名是否为空
        if (null != user) {
            //获取密码
            String password = user.getPassword();
            //权限集合
            List<GrantedAuthority> authorities = new ArrayList<>();
            SimpleGrantedAuthority sai = null;
            //获取用户权限
            Set<Role> roles = user.getRoles();
            if (null != roles) {
                for (Role role : roles) {
                    // 角色用关键字, 授予角色
                    sai = new SimpleGrantedAuthority(role.getKeyword());
                    authorities.add(sai);
                    // 权限, 角色下的所有权限
                    Set<Permission> permissions = role.getPermissions();
                    if (null != permissions) {
                        for (Permission permission : permissions) {
                            // 授予权限
                            sai = new SimpleGrantedAuthority(permission.getKeyword());
                            authorities.add(sai);
                        }
                    }
                }
            }

         /*  return org.springframework.security.core.userdetails.User securityUser =
                    new org.springframework.security.core.userdetails.User(username, user.getPassword(), authorities);*/

            return new org.springframework.security.core.userdetails.User(username,password,authorities);
        }
        return null;
    }

/*    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //数据库查询到的
        User user=findByUsername(username);
        List<GrantedAuthority> authorities= new ArrayList<>();
        GrantedAuthority authority = new SimpleGrantedAuthority("ROLE_ADMIN");
        authorities.add(authority);

        authority = new SimpleGrantedAuthority("add");
        authorities.add(authority);

        org.springframework.security.core.userdetails.User securityUser =
                new org.springframework.security.core.userdetails.User(username,user.getPassword(),authorities);
        return securityUser;

    }
    //假设从数据库查询
    private User findByUsername(String username){
        if ("lzz".equals(username)){
            User user = new User();
            user.setUsername("lzz");
            user.setPassword("$2a$10$u/BcsUUqZNWUxdmDhbnoeeobJy6IBsL1Gn/S0dMxI2RbSgnMKJ.4a");
            //user.setPassword("1234");
            return user;
        }
        return null;
    }
    //测试BCryptPasswordEncoder加密
    public static void main(String[] args) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        // encode加密
        System.out.println(encoder.encode("1234"));

        // 密码匹配:明文,密文
        System.out.println(encoder.matches("1234", "$2a$10$u/BcsUUqZNWUxdmDhbnoeeobJy6IBsL1Gn/S0dMxI2RbSgnMKJ.4a"));
        System.out.println(encoder.matches("1234", "$2a$10$7y4NCbRaAMvdr0RYgs8f5eyiwy28vvOO0.Hcy2neUDt5UpWfpSgTm"));
        System.out.println(encoder.matches("1234", "$2a$10$qzHiyQc.Xn78nkurepFJAeED2A3BIFgnQh/RP89ZeTZMIueb2i4jK"));
        System.out.println(encoder.matches("1234", "$2a$10$XOripk.Z/c2X5.AJdWxrEOSX9vNc9A8jmrDyCcdXcLZS7BGuATq0G"));
    }*/
}
